package com.weihui.jxc.view.operater;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import com.weihui.jxc.common.CommonFactory;
import com.weihui.jxc.common.ValidationManager;
import com.weihui.jxc.model.Operator;
import com.weihui.jxc.service.OperatorService;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * 操作员添加界面
 * @author FPF
 *
 */
public class OperatorAddPanel extends JPanel implements ActionListener{
	private JTextField usernameTf;
	private JTextField nameTf;
	private JTextField passwordTf;
	private JTextField repasswordTf;
	JComboBox comboBox;
	private OperatorService operatorService;
	/**
	 * Create the panel.
	 */
	public OperatorAddPanel() {
		
		JPanel panel = new JPanel();
		
		JPanel panel_1 = new JPanel();
		
		JLabel label = new JLabel("真实姓名：");
		panel_1.add(label);
		
		nameTf = new JTextField();
		nameTf.setColumns(25);
		panel_1.add(nameTf);
		
		JPanel panel_2 = new JPanel();
		
		JLabel label_1 = new JLabel("输入密码：");
		panel_2.add(label_1);
		
		passwordTf = new JTextField();
		passwordTf.setColumns(25);
		panel_2.add(passwordTf);
		
		JPanel panel_3 = new JPanel();
		
		JLabel label_2 = new JLabel("确认密码：");
		panel_3.add(label_2);
		
		repasswordTf = new JTextField();
		repasswordTf.setColumns(25);
		panel_3.add(repasswordTf);
		
		JPanel panel_4 = new JPanel();
		
		JLabel label_3 = new JLabel("用户权限：");
		panel_4.add(label_3);
		
		JPanel panel_5 = new JPanel();
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_5, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel_4, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_5, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(21, Short.MAX_VALUE))
		);
		
		JButton saveBtn = new JButton("添加");
		saveBtn.addActionListener(this);
		panel_5.add(saveBtn);
		
		JButton resetBtn = new JButton("重置");
		resetBtn.addActionListener(this);
		panel_5.add(resetBtn);
		
		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"超级管理员","普通管理员","采购人员","销售人员","仓库人员"}));
		panel_4.add(comboBox);
		
		JLabel lblNewLabel = new JLabel("登  录  名：");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblNewLabel);
		
		usernameTf = new JTextField();
		panel.add(usernameTf);
		usernameTf.setColumns(25);
		setLayout(groupLayout);
		operatorService=CommonFactory.getOperatorService();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String target = e.getActionCommand();
		switch (target) {
		case "添加":
			//非空验证
		Boolean isTrue =	dataCheck(usernameTf.getText(),nameTf.getText().trim(),passwordTf.getText().trim(),repasswordTf.getText().trim());
		if(isTrue) {
			//封装数据
			Operator o = new Operator(usernameTf.getText().trim(), passwordTf.getText().trim(), 
					nameTf.getText().trim(), comboBox.getSelectedItem().toString());
			boolean isFlag = operatorService.saveOperator(o);
			if(isFlag==true) {
				JOptionPane.showMessageDialog(null, "操作员添加成功！","提示",JOptionPane.INFORMATION_MESSAGE);
				setNull();
			}else {
				JOptionPane.showMessageDialog(null, "操作员添加失败！","警告",JOptionPane.WARNING_MESSAGE);
			}
		}else {
			JOptionPane.showMessageDialog(null, "输入数据非法，请认真检查！","警告",JOptionPane.WARNING_MESSAGE);	
		}
//			if(usernameTf.getText().trim()==null || usernameTf.getText().length()==0) {
//				JOptionPane.showMessageDialog(null, "登录名不能为空！","警告",JOptionPane.WARNING_MESSAGE);
//				return;
//			}
//			if(nameTf.getText().trim()==null || nameTf.getText().length()==0) {
//				JOptionPane.showMessageDialog(null, "真实姓名不能为空！","警告",JOptionPane.WARNING_MESSAGE);
//				return;
//			}
//			if(passwordTf.getText().trim()==null || passwordTf.getText().length()==0) {
//				JOptionPane.showMessageDialog(null, "登录密码不能为空！","警告",JOptionPane.WARNING_MESSAGE);
//				return;
//			}
//			if(!repasswordTf.getText().trim().equals(passwordTf.getText().trim())) {
//				JOptionPane.showMessageDialog(null, "两次密码输入不一致！","警告",JOptionPane.WARNING_MESSAGE);
//				return;
//			}
			
			break;
		case "重置":
			setNull();
			break;
		default:
			break;
		}
		
	}
    /**
     * 清空数据
     */
	private void setNull() {
		usernameTf.setText("");
		nameTf.setText("");
		passwordTf.setText("");
		repasswordTf.setText("");
		comboBox.setSelectedIndex(0);
		
	}

	

	public  Boolean dataCheck(String username,String name,String password,String repasswordTf) {
		Boolean isTrue=true;
		if(isTrue==true) {
			if(username==null || username.equals("")) {
				JOptionPane.showMessageDialog(null, "登录名不能为空！","警告",JOptionPane.WARNING_MESSAGE);
				isTrue =false;
				return isTrue;
			}
			if(name==null || name.equals("")) {
				JOptionPane.showMessageDialog(null, "真实姓名不能为空！","警告",JOptionPane.WARNING_MESSAGE);
				isTrue =false;
				return isTrue;
			}
			if(password==null || password.equals("")) {
				JOptionPane.showMessageDialog(null, "登录密码不能为空！","警告",JOptionPane.WARNING_MESSAGE);
				isTrue =false;
				return isTrue;
			}
			if(repasswordTf==null || repasswordTf.equals("")) {
				JOptionPane.showMessageDialog(null, "确认密码不能为空！","警告",JOptionPane.WARNING_MESSAGE);
				isTrue =false;
				return isTrue;
			}
			if(!password.equals(repasswordTf)) {
				JOptionPane.showMessageDialog(null, "两次密码输入不一致！","警告",JOptionPane.WARNING_MESSAGE);
				isTrue =false;
				return isTrue;
			}
		}
			return isTrue;	
		
	}
	
}